﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.DAL;

namespace FilStartUps
{
    public partial class Article : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["Skip"] = 0;
                GetComment(Convert.ToInt32(Request.QueryString["p"].Trim()));
            }

            if (User.Identity.IsAuthenticated)
            {
                LinkButton lkHome = (LinkButton)FilStartUpsPageNav.FindControl("lbLgout");
                LinkButton lkProfile = (LinkButton)FilStartUpsPageNav.FindControl("lbProfile");
                LinkButton lklogin = (LinkButton)FilStartUpsPageNav.FindControl("lblogin");
                LinkButton lkregister = (LinkButton)FilStartUpsPageNav.FindControl("lbRegister");

                UpdatePanel1.Visible = true;
                lklogin.Visible = false;
                lkregister.Visible = false;
                lkProfile.Visible = true;
                lkHome.Visible = true;
            }
        }


        protected void lbHome_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }
        private void GetComment(int postidFK)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var comment = db_connect.Comments
                                     .Where(c => c.postidFK.Equals(postidFK)).ToList();
                ViewState["comment"] = comment;
                BindDataList(Convert.ToInt32(ViewState["Skip"]), postidFK);
                dlcomment.Visible = true;

                if (comment.Count > 10)
                {
                    lnkbNext.Visible = true;
                    lnkbPrev.Visible = true;
                    lnkbPrev.Enabled = false;
                }
                else if (comment.Count == 0)
                {
                    lnkbNext.Visible = false;
                    lnkbPrev.Visible = false;
                    litPageNumber.Visible = false;
                }
                else
                {
                    lnkbNext.Visible = false;
                    lnkbPrev.Visible = false;
                }

            }
        }

        //Pagination
        private void BindDataList(int Skip, int postid)
        {
            int iPage = Skip + 10;
            int currPage = iPage / 10;
            double TotalPage;
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var comment = db_connect.Comments
                                    .Where(c => c.postidFK.Equals(postid))
                                    .OrderByDescending(c => c.dateposted)
                                    .ToList();


                TotalPage = Math.Ceiling((double)comment.Count / 10);

                var postarhieves_ = comment.Skip(Skip).Take(10);

                if (iPage > comment.Count())
                {
                    lnkbNext.Enabled = false;
                }
                else
                {
                    lnkbNext.Enabled = true;
                }

                dlcomment.DataSource = postarhieves_;
                dlcomment.DataBind();

                litPageNumber.Text = "Page " + currPage.ToString().Trim() + " of " + (int)TotalPage;
            }
        }

        protected void lnkbNext_Click(object sender, EventArgs e)
        {
            lnkbPrev.Enabled = true;
            ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) + 10;
            BindDataList(Convert.ToInt32(ViewState["Skip"]), Convert.ToInt32(Request.QueryString["p"].Trim()));
        }

        protected void lnkbPrev_Click(object sender, EventArgs e)
        {
            ViewState["Skip"] = Convert.ToInt32(ViewState["Skip"]) - 10;
            BindDataList(Convert.ToInt32(ViewState["Skip"]), Convert.ToInt32(Request.QueryString["p"].Trim()));
            if (Convert.ToInt32(ViewState["Skip"]) == 0)
            {
                lnkbPrev.Enabled = false;
            }
        }
        protected void btncomment_Click(object sender, EventArgs e)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                Comment comment = new Comment();
                comment.memberidFK = 1;
                comment.dateposted = DateTime.Now;
                comment.postdescription = txtcomment.Text.Trim();
                comment.postidFK = Convert.ToInt32(Request.QueryString["p"].Trim());
                db_connect.Comments.Add(comment);
                db_connect.SaveChanges();
            }
            GetComment(Convert.ToInt32(Request.QueryString["p"].Trim()));
        }
    }
}